package com.youyi.yyfilepickerlibrary.core.loader;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import com.youyi.yyfilepickerlibrary.core.model.Album;
import com.youyi.yyfilepickerlibrary.core.model.EssFile;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class EssMediaCollection implements LoaderManager.LoaderCallbacks<Cursor> {
    private static final String ARGS_ALBUM = "args_album";
    private static final String ARGS_ENABLE_CAPTURE = "args_enable_capture";
    private static final String ARGS_ONLY_SHOWIMAGE = "args_only_showimage";
    private static final int LOADER_ID = 2;
    private Set<EssFile> essFileSet = new LinkedHashSet();
    private EssMediaCallbacks mCallbacks;
    private WeakReference<Context> mContext;
    private LoaderManager mLoaderManager;

    public interface EssMediaCallbacks {
        void onMediaLoad(List<EssFile> list);

        void onmMediaReset();
    }

    @Override // android.support.v4.app.LoaderManager.LoaderCallbacks
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
        Album album;
        Context context = this.mContext.get();
        if (context == null || (album = (Album) bundle.getParcelable(ARGS_ALBUM)) == null) {
            return null;
        }
        return EssMediaLoader.newInstance(context, album);
    }

    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        if (this.mContext.get() != null) {
            ArrayList arrayList = new ArrayList();
            while (cursor.moveToNext()) {
                EssFile essFile = new EssFile(cursor.getLong(cursor.getColumnIndexOrThrow("_id")), cursor.getString(cursor.getColumnIndexOrThrow("mime_type")));
                if (cursor.getLong(cursor.getColumnIndexOrThrow("_id")) == -1) {
                    essFile.setItemType(0);
                } else {
                    if (this.essFileSet.contains(essFile)) {
                        essFile.setChecked(true);
                    }
                    arrayList.add(essFile);
                }
            }
            this.mCallbacks.onMediaLoad(arrayList);
        }
    }

    @Override // android.support.v4.app.LoaderManager.LoaderCallbacks
    public void onLoaderReset(Loader<Cursor> loader) {
        if (this.mContext.get() != null) {
            this.mCallbacks.onmMediaReset();
        }
    }

    public void onCreate(FragmentActivity fragmentActivity, EssMediaCallbacks essMediaCallbacks) {
        this.mContext = new WeakReference<>(fragmentActivity);
        this.mLoaderManager = fragmentActivity.getSupportLoaderManager();
        this.mCallbacks = essMediaCallbacks;
    }

    public void onDestroy() {
        this.mLoaderManager.destroyLoader(2);
        this.mCallbacks = null;
    }

    public void load(Album album) {
        load(album, false, (Set<EssFile>) new LinkedHashSet());
    }

    public void load(Album album, boolean z, Set<EssFile> set) {
        Bundle bundle = new Bundle();
        this.essFileSet = set;
        bundle.putParcelable(ARGS_ALBUM, album);
        bundle.putBoolean(ARGS_ENABLE_CAPTURE, z);
        if (this.mContext.get() == null) {
            this.mLoaderManager.initLoader(2, bundle, this);
        } else {
            this.mLoaderManager.restartLoader(2, bundle, this);
        }
    }

    public void load(Album album, boolean z, boolean z2) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(ARGS_ALBUM, album);
        bundle.putBoolean(ARGS_ENABLE_CAPTURE, z);
        bundle.putBoolean(ARGS_ONLY_SHOWIMAGE, z2);
        if (this.mContext.get() == null) {
            this.mLoaderManager.initLoader(2, bundle, this);
        } else {
            this.mLoaderManager.restartLoader(2, bundle, this);
        }
    }
}
